package com.sx.frame.tools;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.List;

public class DeleteDuplicateDiskFFile {
    public static String path = "F:\\视频\\美剧\\新建文件夹\\";
    public static String recordTxt = "extTorrent.txt";
    public static String recordTxt1 = "downloadTorrent.txt";
    public static String oldTorrentPath = "2\\";
    public static String newTorrentPath = "1\\";

    public static void main(String[] args) {

        List<String> exitsFileName = new ArrayList<>();
        List<String> dupFileName = new ArrayList<>();
        processFile(path,exitsFileName,dupFileName);
        writeFile(path+recordTxt, exitsFileName);
        writeFile(path+recordTxt1, dupFileName);


    }
    public static void writeFile(String path,List<String> exitsFileName){
        try (BufferedWriter bw = new BufferedWriter(new FileWriter(path))) {
            for (String line : exitsFileName) {
                bw.write(line);
                bw.newLine(); // 添加换行符
            }
            System.out.println("文件写入成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void processFile(String filePath,List<String> exitsFileName,List<String> dupFileName){
        File file = new File(filePath);
        for (File f : file.listFiles()){
            if (f.isDirectory()){
                processFile(f.getPath(),exitsFileName,dupFileName);
            }else {
                String[] split = f.getName().split("\\.");
                if(split.length < 1 || split[split.length-1].contains("jpg")||split[split.length-1].contains("torrent")||split[split.length-1].contains("qsed")){
                    continue;
                }
                String s =  processFileName(f.getName());
                if (exitsFileName.contains(s)){
                    dupFileName.add(f.getAbsolutePath());
                }else {
                    exitsFileName.add(s);
                }
            }

        }
    }
    //处理文件中的名字
    public static String processFileName(String fileName) {
        fileName = fileName.toUpperCase().replaceAll("-UNCENSORED", "").replaceAll("-HD","").replaceAll("-AI","");
        // 返回处理后的文件名
        String[] split = fileName.split("-");
        if (split.length < 3){
            if (split.length < 2){
                return split[0];
            }else {
                return split[0]+"-"+split[1];
            }
        }
        return split[0]+"-"+split[1]+"-"+split[2];
    }

    public static void removeFile( String name){
        // 定义源文件路径
        Path source = Paths.get(path+oldTorrentPath+name);
        // 定义目标文件路径
        Path destination = Paths.get(path+newTorrentPath+name);

        try {
            // 移动文件
            Files.move(source, destination, StandardCopyOption.REPLACE_EXISTING);
            System.out.println("文件"+name+"移动成功！");
        } catch (IOException e) {
            // 处理异常
            System.err.println("移动文件时发生错误: " + e.getMessage());
        }
    }
}
